package com.up.upzl.biz.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.up.upzl.biz.domain.Shop;
import com.up.upzl.biz.domain.Shop.Type;
import com.up.upzl.biz.domain.ShopCondition;
import com.up.upzl.biz.service.AreaService;
import com.up.upzl.biz.service.ShopBusinessTypeService;
import com.up.upzl.biz.service.ShopConditionService;
import com.up.upzl.biz.service.ShopService;
import com.up.upzl.biz.vo.ShopListItem;

/**
 * 商铺Api
 * 
 * @author joker
 *
 */
@RestController
@RequestMapping("shop")
public class ShopApi {

	@Autowired
	private AreaService areaService;

	@Autowired
	private Environment env;

	@Autowired
	private ShopConditionService shopConditionService;

	@Autowired
	private ShopBusinessTypeService shopBusinessTypeService;

	@Autowired
	private ShopService shopService;

	/**
	 * 商铺项目列表筛选条件
	 * 
	 * @return 二位数组集合
	 */
	@GetMapping("getConditions")
	public List<Object> getConditions() {
		List<Object> conditionAndValues = Lists.newArrayList();

		List<Object> conditions = Lists.newArrayList();

		List<Object> values = Lists.newArrayList();

		// 区域
		List<String> areas = Lists.newArrayList();
		List<Long> areaValues = Lists.newArrayList();
		Long cityId = null;
		String activeShop = env.getActiveProfiles()[0];
		if (activeShop.contains("bj")) {
			cityId = 36L;
		} else if (activeShop.contains("tj")) {
			cityId = 37L;
		} else if (activeShop.contains("dev")) {
			cityId = 36L;
		} else {
			cityId = 1L;
		}
		areas.add("区域");
		areaValues.add(null);
		areaService.findByParentId(cityId).forEach(area -> {
			areas.add(area.getName());
			areaValues.add(area.getId());
		});
		conditions.add(areas);
		values.add(areaValues);

		// 业态
		List<String> businessTypeConditions = Lists.newArrayList();
		List<Long> businessTypeConditionValues = Lists.newArrayList();

		businessTypeConditions.add("业态");
		businessTypeConditionValues.add(null);
		shopBusinessTypeService.findAll(new Sort(Sort.Direction.DESC, "sort")).forEach(shopBusinessType -> {
			businessTypeConditions.add(shopBusinessType.getName());
			businessTypeConditionValues.add(shopBusinessType.getId());
		});
		conditions.add(businessTypeConditions);
		values.add(businessTypeConditionValues);

		// 面积
		List<String> shopConditionsAreaMeasure = Lists.newArrayList();
		List<Long> shopConditionsValuesAreaMeasure = Lists.newArrayList();

		shopConditionsAreaMeasure.add("面积");
		shopConditionsValuesAreaMeasure.add(null);
		shopConditionService.findByTypeOrderBySortDesc(ShopCondition.Type.面积).forEach(shopCondition -> {
			shopConditionsAreaMeasure.add(shopCondition.getShowText());
			shopConditionsValuesAreaMeasure.add(shopCondition.getId());
		});
		conditions.add(shopConditionsAreaMeasure);
		values.add(shopConditionsValuesAreaMeasure);

		// 价格
		List<Object> priceConditionWrapper = Lists.newArrayList();
		// 价格标签
		priceConditionWrapper.add(Lists.newArrayList("价格"));

		List<Object> priceConditionContainer = Lists.newArrayList();

		// 日租金
		List<String> shopConditionsDailyRent = Lists.newArrayList();
		List<Long> shopConditionsValuesDailyRent = Lists.newArrayList();

		shopConditionsDailyRent.add("日租金");
		shopConditionsValuesDailyRent.add(null);
		shopConditionService.findByTypeOrderBySortDesc(ShopCondition.Type.日租金).forEach(shopCondition -> {
			shopConditionsDailyRent.add(shopCondition.getShowText());
			shopConditionsValuesDailyRent.add(shopCondition.getId());
		});
		values.add(shopConditionsValuesDailyRent);

		priceConditionContainer.add(Lists.newArrayList("日租金", shopConditionsDailyRent));

		// 月租金
		List<String> shopConditionsMonthlyRent = Lists.newArrayList();
		List<Long> shopConditionsValuesMonthlyRent = Lists.newArrayList();

		shopConditionsMonthlyRent.add("月租金");
		shopConditionsValuesMonthlyRent.add(null);
		shopConditionService.findByTypeOrderBySortDesc(ShopCondition.Type.月租金).forEach(shopCondition -> {
			shopConditionsMonthlyRent.add(shopCondition.getShowText());
			shopConditionsValuesMonthlyRent.add(shopCondition.getId());
		});
		values.add(shopConditionsValuesMonthlyRent);

		priceConditionContainer.add(Lists.newArrayList("月租金", shopConditionsMonthlyRent));

		// 转让费
		List<String> shopConditionsTransferFee = Lists.newArrayList();
		List<Long> shopConditionsValuesTransferFee = Lists.newArrayList();

		shopConditionsTransferFee.add("转让费");
		shopConditionsValuesTransferFee.add(null);
		shopConditionService.findByTypeOrderBySortDesc(ShopCondition.Type.转让费).forEach(shopCondition -> {
			shopConditionsTransferFee.add(shopCondition.getShowText());
			shopConditionsValuesTransferFee.add(shopCondition.getId());
		});
		values.add(shopConditionsValuesTransferFee);

		priceConditionContainer.add(Lists.newArrayList("转让费", shopConditionsTransferFee));

		priceConditionWrapper.add(priceConditionContainer);
		conditions.add(priceConditionWrapper);

		conditionAndValues.add(conditions);
		conditionAndValues.add(values);
		return conditionAndValues;
	}

	/**
	 * 商铺列表搜索
	 * 
	 * @param shopId
	 * @param keywords
	 * @param areaId
	 * @param shopBusinessTypeId
	 * @param salePriceId
	 * @param dailyRentId
	 * @param monthlyRentId
	 * @param transferFeeId
	 * @param areaMeasureId
	 * @param shopTypeId
	 * @param shopTagIds
	 * @param status
	 * @param page
	 * @return
	 */
	@GetMapping("findByCondition")
	public Page<ShopListItem> findByCondition(Long shopId, String keywords, Long areaId, Long shopBusinessTypeId,
			Long salePriceId, Long dailyRentId, Long monthlyRentId, Long transferFeeId, Long areaMeasureId,
			String shopType, Long[] shopSupportingId, Long[] shopTagIds, Pageable page) {
		return null;
	}

	/**
	 * 获取商铺类型
	 * 
	 * @return
	 */
	@GetMapping("getShopTypes")
	public Type[] getShopTypes() {
		return Shop.Type.values();
	}


	
	public Map<String, Object> findByTop(Long next, Integer pageSize) {
		Map<String, Object> msg = Maps.newHashMap();
		Pageable pageable = new PageRequest(0, pageSize);
		List<Shop> list = shopService.findByAppShop(next, pageable);
		Date createTime = list.get(list.size() - 1).getCreateTime();
		msg.put("code", "200");
		msg.put("content", list);
		msg.put("next", createTime.getTime());
		msg.put("msg", "success");
		return msg;
	}
	
	@PostMapping("getShopList")
	public Map<String, Object> findByAppShop(Long next) {
		Map<String, Object> msg = Maps.newHashMap();
		Pageable pageable = new PageRequest(0, 10,Direction.DESC,"id");
		List<Shop> list = shopService.findByAppShop(next, pageable);
		if(list.size()==10) {
			Date createTime = list.get(list.size() - 1).getCreateTime();
			msg.put("next", createTime.getTime());			
		}
		List<Map<String,Object>> newList=Lists.newArrayList();
        list.forEach(s->{
        	Map m=Maps.newHashMap();
        	m.put("", s.getId());
        	m.put("", s.getName());
        	m.put("", s.getShopTags());
        	m.put("", s.getMonthlyRent());
        	m.put("", "");
        	m.put("", "");
        	m.put("", "");
        	m.put("", "");
        	m.put("", "");
        	m.put("", "");
        	m.put("", "");
        	
//        	shop.setId();
//        	shop.setName();
//        	shop.setShopTags();
//        	shop.setArea(s.getArea());
//        	shop.setMonthlyRent();
//        	//建筑面积
//        	shop.setBuildArea(s.getBuildArea());
//        	shop.setBusinessArea(s.getBusinessArea());
//        	if(StringUtils.isNotBlank(s.getImgs())) {
//        		shop.setImgs(s.getImgs().split(",")[0]);		
//        	}
//        	newList.add(shop);
        	
        });
		msg.put("code", "200");
		msg.put("content", newList);
		msg.put("msg", "success");
		return msg;
	}
	
	/**
	 * 查
	 * 
	 * @param next
	 * @param pageSize
	 * @return
	 */
	public Map<String, Object> findByAppDjsk(Long next, Integer pageSize) {
		Map<String, Object> msg = Maps.newHashMap();
		Pageable pageable = new PageRequest(0, pageSize);
		List<Shop> list = shopService.findByAppShop(next, pageable);
		Date createTime = list.get(list.size() - 1).getCreateTime();
		msg.put("code", "200");
		msg.put("content", list);
		msg.put("next", createTime);
		msg.put("message", "success");
		return msg;
	}
}
